/*//////////////////////////////////////////////////////////
 /// Demo of MVC implementation in javascript UI layer /////
 /// author - Alex Li //////////////////////////////////////
 //////////////////////////////////////////////////////////*/

/**
 ** Controller for editable grid
 **/
var EditableDatagridController = Class.create();
EditableDatagridController.prototype = Class.extend({
		/**
		 ** Callback for header click event
		 **/
		On_header_click:function(request){
			  var EventElement = request.Event.target||request.Event.srcElement
			  var sortBy = EventElement.parentNode.id.replace(request.view.id+"_th_","");
			  request.model.SortBy(sortBy);
		},
		/**
		 ** Callback for cell click event
		 **/
		On_cell_click:function(request){
			  var EventElement = request.Event.target||request.Event.srcElement
			  var row = (EventElement.id=="")?EventElement.parentNode.parentNode:EventElement;
			  if(row.id.indexOf("td")!=-1) return;
			  var tds = row.getElementsByTagName("td");
			  var trs = request.view.Container.getElementsByTagName("tr");
			  for(i=0;i<trs.length;i++){
			  	var tds2 = trs[i].getElementsByTagName("td");
			  	for(j=0;j<tds2.length;j++){
				  	if(tds2[j].childNodes[0].childNodes[0].tagName == "INPUT" || tds2[j].childNodes[0].childNodes[0].tagName == "input"){
				  		tds2[j].childNodes[0].innerHTML  = request.model.Get((i-1),request.view.GetDataIndex(j));
				  	}
				  }
			  }
			  
			  for(i=0;i<tds.length;i++){
			  	tds[i].childNodes[0].innerHTML  = "<input value='"+tds[i].childNodes[0].innerHTML +"' style='width:"+request.view.HeaderArray[i].width+"px;'>";
				if (i == tds.length-1) {
					tds[i].childNodes[0].innerHTML += "<button id='btnSave'>Save</button>";
				}				
			  }
			  
			  // Save row changes
			  var btnSave = document.getElementById("btnSave");
			  if (btnSave) {
			      btnSave.onmousedown = function() {
				      for(i=0;i<trs.length;i++){
						var tds2 = trs[i].getElementsByTagName("td");
						for(j=0;j<tds2.length;j++){
							if(tds2[j].childNodes[0].childNodes[0].tagName == "INPUT" || tds2[j].childNodes[0].childNodes[0].tagName == "input"){
								var new_value = tds2[j].childNodes[0].childNodes[0].value;
								request.model.Set((i-1),request.view.GetDataIndex(j),new_value);
							}
						  }
					  }
				      request.model.Refresh();
				  }
			  }
		}
},Controller.prototype);

/**
 ** Controller for selectable grid
 **/
var SelectableDatagridController = Class.create();
SelectableDatagridController.prototype = Class.extend({
		/**
		 ** Override the initialize contructor
		 **/
		initialize: function(config){
			this.associateFieldSet = config.associateFieldSet;
			this.allocateProperty(config);
		},
		
		/**
		 ** Callback for cell click event
		 **/
		On_cell_click:function(request){
			var EventElement = request.Event.target||request.Event.srcElement
			var row = (EventElement.id=="")?EventElement.parentNode.parentNode:EventElement;
			if(row.id.indexOf("td")!=-1) return;
            var index = Number(row.id.replace(request.view.id+"tr_",""));	
			this.associateFieldSet.KeyValue = index;
			this.associateFieldSet.Render();
		}
},Controller.prototype);

/////////////////Add more controller here to enjoy the game...//////////////////////////